<template>
  <div>
    <el-button type="primary" @click="add">
      <el-icon :size="14">
        <plus />
      </el-icon>
      添加
    </el-button>
  </div>
  <el-divider />
  <div>
    <el-table :data="menus" style="width: 100%" row-key="id" border highlight-current-row @current-change="handleCurrentChange">
      <el-table-column prop="id" label="编号" sortable width="100"></el-table-column>
      <el-table-column prop="title" label="标题" sortable width="180"></el-table-column>
      <el-table-column prop="name" label="组件名称"></el-table-column>
      <el-table-column prop="path" label="访问路径"></el-table-column>
      <el-table-column prop="perms" label="权限名称"></el-table-column>
      <el-table-column prop="hasPerms" label="访问权限"></el-table-column>
      <el-table-column prop="component" label="前端组件"></el-table-column>
      <el-table-column prop="icon" label="图标"></el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button type="warning" plain size="small" @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger" plain size="small" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!--编辑对话框-->
  <el-dialog title="编辑角色信息" v-model="dialogVisible" width="30%" @close="closeDialog">
    <div>
      <el-form :model="menuInfo" :rules="rules" ref="menuInfo" label-width="100px">
        <el-form-item label="组件名称">
          <el-input v-model="menuInfo.name" placeholder="请输入组件名称"></el-input>
        </el-form-item>
        <el-form-item label="组件路径">
          <el-input v-model="menuInfo.component" placeholder="请输入组件路径"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="menuInfo.icon" placeholder="请输入图标名称"></el-input>
        </el-form-item>
        <el-form-item label="序号">
          <el-input v-model="menuInfo.orderNum" placeholder="请输入菜单序号"></el-input>
        </el-form-item>
        <el-form-item label="父节点">
          <el-select v-model="menuInfo.parentId" placeholder="请选择父节点">
            <el-option style="height: auto;padding: 0;">
              <el-tree :data="menus"
                       :props="treeProps"
                       node-key="id"
                       :default-expand-all="true"
                       highlight-current>
              </el-tree>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单标题" prop="title">
          <el-input v-model="menuInfo.title" placeholder="请输入菜单标题"></el-input>
        </el-form-item>
        <el-form-item label="path">
          <el-input v-model="menuInfo.path" placeholder="请输入菜单路径"></el-input>
        </el-form-item>
        <el-form-item label="perms" prop="perms">
          <el-input v-model="menuInfo.perms" placeholder="请输入访问权限"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
    <span class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
    </span>
    </template>
  </el-dialog>
</template>

<script>
import {defineComponent} from "vue";
import {newMenu, getMenuList} from "@/api/menus";
import {deleteMenuById} from "../../api/menus";
import {ElMessage} from "element-plus";

export default defineComponent({
  name: 'Menus',
  setup() {
    // 部门树属性
    const treeProps = {
      children: 'children',
      label: 'title',
    }
    return {
      newMenu,
      getMenuList,
      treeProps
    }
  },
  data() {
    return {
      menus: [],
      dialogVisible: false,
      menuInfo: null,
      rules: {
        title: [{ required: true, message: '请输入菜单标题', trigger: 'blur' }],
        perms: [{ required: true, message: '请输入访问权限', trigger: 'blur' }],
      },
      currentRow: null
    }
  },
  created() {
    this.getMenus()
  },
  methods: {
    // 获取菜单树
    getMenus() {
      getMenuList().then(res => {
        this.menus = res.data
        console.log(this.navs)
      })
    },
    add() {
      this.menuInfo = this.newMenu
      this.dialogVisible = true
    },
    edit(row) {
      this.menuInfo = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    del(id) {
      this.$confirm('确认删除菜单信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteMenuById(id).then(res => {
          ElMessage.success(res.msg);
        })
      }).catch(() => {
        ElMessage.info('已取消删除')
      })
    },
    // 选择当前行
    handleCurrentChange(val) {
      this.currentRow = val
    },
    closeDialog() {
      this.$refs['menuInfo'].resetFields()  // 清空验证信息
    }
  },
  computed: {
  }
})
</script>

<style>

</style>